package com.xo.so.vanthan;

import java.util.ArrayList;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.message.Message;
import com.xo.so.R;
import com.xo.so.Variables;
import com.xo.so.adapter.ItemListProvinceAdapter;
import com.xo.so.jsouplocal.JsoupProvince;

public class FragmentMienTrung extends Fragment {

	private static final String[] tenCacTinh = { "Quảng Trị", "Thừa Thiên Huế",
			"Đà Nẵng", "Ninh Thuận", "Khánh Hòa", "Phú Yên", "Quảng Nam",
			"Quảng Ngãi", "Quảng Bình", "Kon Tum", "Đắc Nông", "Bình �?ịnh",
			"Gia Lai", "ĐăkLắc" };
	private ProgressDialog mProgressDialog;

	private ItemListProvinceAdapter madapter;
	private ListView list;
	public static final String TINH_ID = "id tinh";

	public FragmentMienTrung() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_mien_trung,
				container, false);

		return rootView;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		list = (ListView) view.findViewById(R.id.listView_mien_trung);
		madapter = new ItemListProvinceAdapter(getActivity(),
				R.layout.item_list_tinh_custome,
				new ArrayList<ItemListProvinceEntity>());

		list.setAdapter(madapter);
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Fragment fragment = new DataMienTrungFragment();

				if (fragment != null) {
					FragmentManager fragmentManager = getFragmentManager();
					Bundle bundle = new Bundle();
					bundle.putString(TINH_ID, tenCacTinh[position]);
					fragment.setArguments(bundle);
					fragmentManager.beginTransaction()
							.replace(R.id.container, fragment).commit();
				}

			}
		});
		JsoupMTrung jMienTrung = new JsoupMTrung();
		jMienTrung.execute(Variables.site);
		super.onViewCreated(view, savedInstanceState);
	}

	public class JsoupMTrung extends AsyncTask<String, Void, Void> {
		private ArrayList<ItemListProvinceEntity> mItemProvince;
		private JsoupProvince jProvince;

		@Override
		protected void onPreExecute() {
			jProvince = new JsoupProvince();
			mItemProvince = new ArrayList<ItemListProvinceEntity>();
			mProgressDialog = new ProgressDialog(getActivity());
			mProgressDialog.setTitle("Please waiting ...");
			mProgressDialog.setMessage("Loading...");
			mProgressDialog.setIndeterminate(false);
			mProgressDialog.show();
		}

		@Override
		protected Void doInBackground(String... params) {

			mItemProvince = jProvince.parseJsoupProvince(params[0], 2);

			return null;

		}

		@Override
		protected void onProgressUpdate(Void... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(Void result) {

			if (mItemProvince == null) {
				Message message = new Message(getActivity());
				message.DialogExit();
			} else {
				madapter.addAll(mItemProvince);
				mProgressDialog.dismiss();
				super.onPostExecute(result);
			}
		}

	}

}
